﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FineSoft.Models
{
	public static partial class InitialData
	{
		//public static Class[] Classes;

		//public static Class GetClass(string name)
		//{
		//    return Classes.First(c=>c.Name==name||c.AlternateNames.Contains(name));
		//}

		static InitialData()
		{
			//Class.CustomCode.Index = Classes.Length;
			//Classes = Classes.Concat(new[] { Class.CustomCode }).ToArray();

			ComponentClass grid = ((ComponentClass)Classes.First(c => c.Name == "Ext.grid.Panel"));
			Config columnsConfig = grid.Configs.First(c => c.Name == "columns");
			columnsConfig.DefaultValue = new ArrayObject { Type = Class.Object };

			ComponentClass cycle = ((ComponentClass)Classes.First(c => c.Name == "Ext.button.Cycle"));
			Config menuConfig = cycle.Configs.First(c => c.Name == "menu");
			menuConfig.DefaultValue = new NativeObject { Type = Class.String, Value = "" };

			ComponentClass chart = ((ComponentClass)Classes.First(c => c.Name == "Ext.chart.Chart"));
			Config storeConfig = chart.Configs.First(c => c.Name == "store");
			ExtObject storeObject = new ExtObject { Type = Classes.First(c => c.Name == "Ext.data.Store") };
			storeObject.Configs["data"] = new ArrayObject { Type = Class.Object };
			storeObject.Configs["proxy"] = new ExtObject { Type = Classes.First(c => c.Name == "Ext.data.proxy.Ajax") };
			storeConfig.DefaultValue = storeObject;

			ComponentClass view = ((ComponentClass)Classes.First(c => c.Name == "Ext.view.View"));
			Config tplConfig = view.Configs.First(c => c.Name == "tpl");
			tplConfig.DefaultValue = new ExtObject { Type = Classes.First(c => c.Name == "Ext.XTemplate") };
		}

		public static Class GetClass(System.Collections.Generic.IEnumerable<Class> classes, string name)
		{
			return classes.FirstOrDefault(c => c.Name == name || (c is ExtClass && ((ExtClass)c).AlternateNames.Contains(name)) || (c is ArrayClass && ((ArrayClass)c).AlternateNames.Contains(name)));
		}

		public static Class GetClass(string name)
		{
			return GetClass(Classes, name);
		}

		private static ComponentClass[] _VisibleComponents;
		private static ComponentClass[] VisibleComponents
		{
			get
			{
				if (_VisibleComponents == null)
					_VisibleComponents = Classes.Where(c => !c.Private && c is ComponentClass).OrderBy(c => c.Name.Substring(0, c.Name.LastIndexOf('.'))).Select(c => (ComponentClass)c).ToArray();
				return _VisibleComponents;
			}
		}

		private static NameSpace[] _VisibleNameSpaces;
		public static NameSpace[] VisibleNameSpaces
		{
			get
			{
				if (_VisibleNameSpaces == null)
				{
					System.Collections.Generic.List<NameSpace> nameSpaces = new List<NameSpace>();
					foreach (NameSpace nameSpace in VisibleComponents.Select(c => new NameSpace(c.Name.Substring(0, c.Name.LastIndexOf('.')))))
						if (nameSpaces.Count == 0 || nameSpaces[nameSpaces.Count - 1].FullName != nameSpace.FullName)
							nameSpaces.Add(nameSpace);
					foreach (NameSpace nameSpace in nameSpaces)
					{
						nameSpace.NameSpaces = nameSpaces.Where(n => n.FullName != "Ext" && n.FullName.Substring(0, n.FullName.LastIndexOf('.')) == nameSpace.FullName).ToArray();
						nameSpace.Components = VisibleComponents.Where(c => c.Name.Substring(0, c.Name.LastIndexOf('.')) == nameSpace.FullName).ToArray();
					}
					_VisibleNameSpaces = nameSpaces.ToArray();
				}
				return _VisibleNameSpaces;
			}
		}

		private static NameSpace _ExtNameSpace;
		public static NameSpace ExtNameSpace
		{
			get
			{
				if (_ExtNameSpace == null)
					_ExtNameSpace = VisibleNameSpaces.First(n => n.FullName == "Ext");
				return _ExtNameSpace;
			}
		}

		//private static void CreateNameSpace(NameSpace extNameSpace, string fullName)
		//{
		//}
	}
}
